import request from './request'
import axios from 'axios'
import { BASE_URL } from './baseurl'

export function getUserList(params) {
  return request({
    url: '/api/User',
    method: 'get',
    params
  })
}

export function createUser(data) {
  return request({
    url: '/api/User',
    method: 'post',
    data
  })
}

// 获取用户信息
export const getUserInfo = async (userId) => {
  try {
    const response = await request({
      url: `/api/User/${userId}`,
      method: 'get'
    })
    return response
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取用户信息失败')
  }
}

// 获取用户头像文件名
export const getUserAvatarFileName = async (userId) => {
  try {
    const response = await request({
      url: `/api/Image/avatar/${userId}`,
      method: 'get'
    })
    return response
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取用户头像失败')
  }
}

// 更新用户头像
export const updateUserAvatar = async (userId, formData) => {
  try {
    const response = await request({
      url: `/api/user/${userId}/avatar`,
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    return response
  } catch (error) {
    throw new Error(error.response?.data?.message || '更新用户头像失败')
  }
}

// 批量获取用户头像
export async function getUserAvatarsBatch(userIds) {
  const token = localStorage.getItem('token')
  try {
    const res = await axios({
      url: BASE_URL + '/api/Image/avatars-by-userids',
      method: 'post',
      data: userIds,
      headers: {
        'Content-Type': 'application/json',
        Authorization: token ? `Bearer ${token}` : ''
      }
    })
    return res
  } catch (e) {
    console.error('批量头像接口异常:', e)
    throw e
  }
}

export async function getUserContactInfo(userId, token) {
  const res = await axios.get(`${BASE_URL}/api/user/${userId}/contact-info`, {
    headers: { Authorization: `Bearer ${token}` }
  })
  return res.data
} 